﻿/*
 * Copyright (c) 2011, The Chancellor, Masters and Scholars of the University of Oxford
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of 
 *   conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this list 
 *   of conditions and the following disclaimer in the documentation and/or other 
 *   materials provided with the distribution.
 *
 * Neither the name of University of Oxford nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior 
 * written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Dicom.Data;
using System.Text;
using Dicom;
using System.IO;
//using Vreci.Workarounds;
using Dicom.Imaging;
using System.Xml;
using System.Xml.Linq;
using System.Drawing;
using Vreci.Metadata;

namespace Vreci.FileEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class FileEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {

            //See if it ends with .dcm - If so then try and get the metadata (And put into a .metadata.xml file) and the image and put into a .png file.

            SPFile file = properties.ListItem.File;

            if (file != null)
            {
                //Check to see that the directory is not the Hidden Shaddow One
                if (file.InDocumentLibrary)
                {
                    SPDocumentLibrary document_library = (SPDocumentLibrary)properties.List;

                    //We don't want to deal with any entries to the Shaddow Folder:
                    //The list has a unique guid FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101"
                    if (document_library.TemplateFeatureId.ToString() != "{00bfea71-e717-4e80-aa17-d0c71b360101}")
                    {
                        if (file.Name.EndsWith(".dcm"))
                        {
                            //We have a .dcm file - do some processing
                            var Generator = new GenerateFromDicom(properties.Web, document_library, file);
                            Generator.ProcessDCM();
                        }
                    }
                }
            }

            base.ItemAdded(properties);
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
        }


        public override void  ItemDeleting(SPItemEventProperties properties)
        {
            /*
            //Stop events listening to the deltions we make here
            bool enabled = base.EventFiringEnabled;
            base.EventFiringEnabled = false;

            try
            {

                SPFile file = properties.ListItem.File;

                if (file != null)
                {
                    //Check to see that the directory is not the Hidden Shaddow One
                    if (file.InDocumentLibrary)
                    {
                        SPDocumentLibrary document_library = (SPDocumentLibrary)properties.List;

                        //We don't want to deal with any entries to the Shaddow Folder:
                        //The list has a unique guid FeatureId="00bfea71-e717-4e80-aa17-d0c71b360101"
                        if (document_library.TemplateFeatureId.ToString() != "{00bfea71-e717-4e80-aa17-d0c71b360101}")
                        {
                            if (file.Name.EndsWith(".dcm"))
                            {
                                //We have a .dcm file - do some processing
                                var Generator = new GenerateFromDicom(properties.Web, document_library, file);
                                Generator.RemoveDCM();
                            }
                        }
                    }
                }
            }
            finally
            {
                base.EventFiringEnabled = enabled;
            }
             * */
            base.ItemDeleting(properties);

        }
       

        /// <summary>
        /// A file was moved.
        /// </summary>
        public override void ItemFileMoved(SPItemEventProperties properties)
        {
            base.ItemFileMoved(properties);
        }


    }
}
